package com.mmw;

import com.mmw.entity.Employee;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author mamengwei
 */
public class AppJdbc {
    static List<Employee> employees = new ArrayList<>();
    static Scanner scanner = new Scanner(System.in);
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
            System.out.println("漂亮团外卖系统");
            while (true){

                System.out.println("1.商家登录");
                System.out.println("2.商家注册");
                System.out.println("请输入操作命令:");

                int select = scanner.nextInt();
                switch (select){
                    case 1:login();break;
                    case 2:register();break;
                }
            }

    }
    static Connection getConn() throws ClassNotFoundException, SQLException{
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/regis_takeaway", "root", "root");
        return conn;
    }
    static void login() throws SQLException, ClassNotFoundException {
        Connection conn = getConn();
        System.out.println("请输入用户名:");
        String username = scanner.next();
        System.out.println("请输入密码:");
        String password = scanner.next();
        String sql = "select * from employee where username = ? and password = ?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1,username);
        ps.setString(2,password);
        ResultSet rs = ps.executeQuery();
        if(rs.next()){
            logined();
        }else{
            System.out.println("用户名密码错误");
        }
    }

    static void logined() throws SQLException, ClassNotFoundException {
        System.out.println("欢迎来到商家页面");
        int flag = 1;

        while (true){
            if(flag == 2){
                break;
            }
            System.out.println("1.员工管理");
            System.out.println("2.分类管理");
            //套餐管理和订单管理同上
            System.out.println("3.退出商家页面");
            System.out.println("请输入你的选择:");
            int select = scanner.nextInt();
            switch (select){
                case 1:employeManage();break;
                case 2:classificationManage();break;
                case 3:flag = 2;break;
            }
        }
    }
    static void register() throws SQLException, ClassNotFoundException {
        System.out.println("请输入用户名:");
        String username = scanner.next();
        System.out.println("请输入密码:");
        String password = scanner.next();
        Connection conn = getConn();
        String sql_select = "select * from employee where username = ?";
        PreparedStatement ps = conn.prepareStatement(sql_select);
        ps.setString(1,username);
        ResultSet rs_select = ps.executeQuery();
        if (rs_select.next()){
            System.out.println("用户名已存在!");
            return;
        }
        String sql = "insert into employee(username,password) values(?,?)";
        PreparedStatement rs = conn.prepareStatement(sql);
        rs.setString(1,username);
        rs.setString(2,password);
        int n = rs.executeUpdate();
        if (n==1) {
            System.out.println("注册成功");
        }else {
            System.out.println("注册失败,数据库执行错误");
        }
    }
    static void employeManage() throws SQLException, ClassNotFoundException {
        int flag = 1;
        while (true){
            if (flag == 2){
                break;
            }
            System.out.println("1.查询员工");
            System.out.println("2.增加员工");
            System.out.println("3.删除员工");
            System.out.println("4.退出员工管理");
            System.out.println("请输入你的选择:");
            int select = scanner.nextInt();
            switch (select){
                case 1:showEmployee();break;
                case 2:addEmployee();break;
                case 3:deleteEmployee();break;
                case 4:flag = 2;break;
            }
        }
    }
    static void classificationManage() throws SQLException, ClassNotFoundException {
        int flag = 1;
        while (true){
            if (flag == 2){
                break;
            }
            System.out.println("1.查询分类");
            System.out.println("2.增加分类");
            System.out.println("3.删除分类");
            System.out.println("4.退出分类管理");
            System.out.println("请输入你的选择:");
            int select = scanner.nextInt();
            switch (select){
                case 1:showCategory();break;
                case 2:addCategory();break;
                case 3:deleteCategory();break;
                case 4:flag = 2;break;
            }
        }
    }

    private static void showCategory() throws SQLException, ClassNotFoundException {
        Connection conn = getConn();
        String sql = "select * from category";
        PreparedStatement ps = conn.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();
        while (rs.next()){
            System.out.println("ID:"+rs.getLong("id")+",名称:"+rs.getString("name"));
        }

    }
    private static void addCategory() throws SQLException, ClassNotFoundException{
        System.out.println("请输入分类名称");
        String name = scanner.next();
        String sql = "insert into category(name) value (?)";
        Connection conn = getConn();
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1,name);
        ps.executeUpdate();
        System.out.println("添加成功");
    }
    private static void deleteCategory() throws SQLException, ClassNotFoundException{
        showCategory();
        System.out.println("请输入要删除分类的ID");
        long id = scanner.nextLong();
        String sql = "delete from category where id = ?";
        Connection conn = getConn();
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setLong(1,id);
        int i = ps.executeUpdate();
        System.out.println("删除成功");
    }

    static void showEmployee() throws SQLException, ClassNotFoundException {
        Connection conn = getConn();
        String sql = "select * from employee";
        PreparedStatement ps = conn.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();
        while (rs.next()){
            System.out.println("ID:"+rs.getInt("id")+",姓名:"+rs.getString("username"));
        }
    }
    static void addEmployee()throws SQLException, ClassNotFoundException{
        System.out.println("请输入员工用户名");
        String username = scanner.next();
        System.out.println("请输入员工密码");
        String password = scanner.next();
        Connection conn = getConn();
        String sql = "insert into employee(username,password) values (?,?)";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1,username);
        ps.setString(2,password);
        int n = ps.executeUpdate();
        System.out.println("添加成功");
    }
    static void deleteEmployee()throws SQLException, ClassNotFoundException{
        showEmployee();
        System.out.println("请输入删除员工共的id");
        int id = scanner.nextInt();
        Connection conn = getConn();
        String sql = "delete from employee where id = ?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setInt(1,id);
        int i = ps.executeUpdate();
        System.out.println("删除成功");

    }

}
